package com.itany.servlet;

import com.alibaba.fastjson.JSON;
import com.itany.entity.Account;
import com.itany.util.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Author：汤小洋
 * Date：2022-11-14 15:02
 * Description：<描述>
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        /**
         * 通过CORS允许跨域访问
         */
        resp.setHeader("Access-Control-Allow-Origin","*");

        resp.setContentType("application/json;charset=utf8");
        PrintWriter out = resp.getWriter();

        ResponseResult result;
        if("tom".equals(username) && "123".equals(password)){
            Account account = new Account(1001, username, password, "汤姆");
            result = ResponseResult.success(account);
        }else{
             result = ResponseResult.fail("登陆失败");
        }

        out.print(JSON.toJSONString(result));
        out.close();
    }
}
